/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ac.pbp.controller;

import ac.pbp.core.exception.ServiceException;
import ac.pbp.core.util.ViewHelper;
import ac.pbp.dto.UserDto;
import ac.pbp.dto.filter.UserFilterCriteria;
import ac.pbp.model.User;
import ac.pbp.service.UserService;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.catalina.util.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author Kim
 */
@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/addUser.html")
    public ModelAndView addUser(@ModelAttribute("user") User user, HttpServletRequest request, HttpServletResponse response) throws ServiceException {
        
        Long lastId = userService.getMax() + 1;
        String kodeUser = String.format("U%03d", lastId);
        user.setKodeUser(kodeUser);
        userService.addUser(user);

        request.getSession(false).setAttribute("name", user.getDisplayName());
        request.getSession(false).setAttribute("kodeUser", user.getKodeUser());
        request.getSession(false).setMaxInactiveInterval(3600);

        return ViewHelper.redirectTo("home.html");
    }
    
    @RequestMapping("/friends.html")
    public ModelAndView friends(HttpServletRequest request) {
        
        ModelAndView model = new ModelAndView("friends");
        
        return model;
    }
    
    @RequestMapping("/profile.html")
    public ModelAndView profile(HttpServletRequest request) {
        
        String kodeUser = null;
        
        ModelAndView model = new ModelAndView("profile");
        
        kodeUser = request.getParameter("kodeUser");
        
        if (kodeUser == null) {
            kodeUser = (String) request.getSession(false).getAttribute("kodeUser");
            model.addObject("own", "own");
        }
        
        User user = userService.loadUser(kodeUser);
        
        model.addObject("user", user);
        
        return model;
    }
    @RequestMapping("/editProfile.html")
    public ModelAndView editProfile(HttpServletRequest request) {
        
        ModelAndView model = new ModelAndView("editProfile");
         String kodeUser = (String) request.getSession(false).getAttribute("kodeUser");
        
        
        User user = userService.loadUser(kodeUser);
        model.addObject("user",user);
        model.addObject("userList", userService.loadAll());
        return model;
    }
    
    @RequestMapping(value = "/interestedAdd.html", method = RequestMethod.POST)
    public ModelAndView interestedAdd(@ModelAttribute("user") User user, HttpServletRequest request, HttpServletResponse response) throws ServiceException {

        String kodeUser = (String) request.getSession(false).getAttribute("kodeUser");
        userService.updateUserInterest(user, kodeUser);

        return ViewHelper.redirectTo("recommendations.html");
    }
    
    
    @RequestMapping("/userView.html")
    public ModelAndView viewUser(HttpServletRequest request) {
        
        String page = request.getParameter("page");
        String mode = request.getParameter("mode");

        if ((page == null) || page.equals("")) {
            page = "1";
        }

        UserFilterCriteria filter = new UserFilterCriteria();
        filter.setPage(Integer.valueOf(page));
        filter.setMax(10);
        
        String dataSearch = null;
        if (mode != null) {
            if (mode.equals("search")) {
                dataSearch = request.getParameter("dataSearch");
                if (!dataSearch.equals("")) {
                    filter.setDataSearch(dataSearch);
                }
            }
        }

        UserDto userDto = userService.filterUser(filter);
        
        ModelAndView model = new ModelAndView("userView");
        model.addObject("data", userDto);
        model.addObject("dataSearch", dataSearch);
        
        return model;
    }
    
    @RequestMapping("/userPrepareAdd.html")
    public ModelAndView prepareAddUser(HttpServletRequest request) {
        
        ModelAndView model = new ModelAndView("userAdd");
        
        model.addObject("userList", userService.loadAll());
        
        return model;
    }
    
  
    
    @RequestMapping("/userPrepareUpdate.html")
    public ModelAndView prepareUpdateUser(HttpServletRequest request) {
        
        String kodeUser = request.getParameter("kodeUser");
        
        ModelAndView model = new ModelAndView("userAdd");
        User user = userService.loadUser(kodeUser);
        model.addObject("user",user);
        model.addObject("userList", userService.loadAll());
        
        return model;
    }
    
    @RequestMapping(value = "/userUpdate.html", method = RequestMethod.POST)
    public ModelAndView updateUser(@ModelAttribute("user")  User user, HttpServletRequest request, HttpServletResponse response) throws ServiceException {
        
        userService.updateUser(user);
        
        return ViewHelper.redirectTo("userView.html");
    }
    
    @RequestMapping("/userDelete.html")
    public ModelAndView deleteUser(HttpServletRequest request) {
        String idArray = request.getParameter("idArray");
        userService.deleteUser(idArray);
        
        return ViewHelper.redirectTo("userView.html");
    }
    
    @RequestMapping("/userAdd.html")
    public ModelAndView userAdd(@ModelAttribute("user") User user, HttpServletRequest request, HttpServletResponse response) throws ServiceException {
        
        userService.addUser(user);
        
        return ViewHelper.redirectTo("userView.html");
    }
    
    @RequestMapping("/saveProfile.html")
   public ModelAndView saveProfile(@RequestParam("photo") MultipartFile fileGambar, HttpServletRequest request, HttpServletResponse response) throws ServiceException, IOException {
        
        String photo = Base64.encode(fileGambar.getBytes());
        String firstName=request.getParameter("firstName");
        String lastName=request.getParameter("lastName");
        String displayName=request.getParameter("displayName");
        String gender=request.getParameter("gender");
        String kodeUser = (String) request.getSession(false).getAttribute("kodeUser");
        User user = userService.loadUser(kodeUser);
        System.out.println("################### " + photo);
        user.setFirstName(firstName);
        user.setLastName(lastName);
        user.setDisplayName(displayName);
        user.setGender(gender);
        user.setPhoto(photo);
        
        userService.updateUser(user);
        
        return ViewHelper.redirectTo("profile.html");
    }
    
    
   
}
